package com.proxyIp;

import android.content.DialogInterface;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;
import android.text.InputType;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.Toast;

public class ProxyIpSettings extends PreferenceActivity implements
        OnPreferenceChangeListener, DialogInterface.OnClickListener {

    private static final String IP = "ip";
    private static final String PORT = "port";
    private static final String IP_ADD = "ip_add";
    private static final String IP_LIST = "ip_list";
    private static final int POSITIVE_BUTTON = -1;
    private static final int EDIT = 1;
    private static final int DELETE = 2;

    private EditTextPreference mIpText, mPortText;
    private PreferenceCategory mIpList;
    private Preference mIpAdd;
    private NoProxyIp mTempPreference;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.proxy_ip_settings);

        mIpText = (EditTextPreference) findPreference(IP);
        mIpText.setPositiveButtonText(R.string.confirm);
        mIpText.setNegativeButtonText(R.string.cancel);
        mIpText.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);

        mPortText = (EditTextPreference) findPreference(PORT);
        mPortText.setPositiveButtonText(R.string.confirm);
        mPortText.setNegativeButtonText(R.string.cancel);
        mPortText.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);

        mIpText.setOnPreferenceChangeListener(this);
        mIpText.setSummary(mIpText.getText());

        mPortText.setOnPreferenceChangeListener(this);
        mPortText.setSummary(mPortText.getText());

        mIpAdd = (Preference) findPreference(IP_ADD);

        mIpList = (PreferenceCategory) findPreference(IP_LIST);

        registerForContextMenu(getListView());

    }

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
            Preference preference) {
        if (preference == mIpAdd) {
            NoProxyIpDialog mNoProxyIpDialog = new NoProxyIpDialog(this, this);
            mNoProxyIpDialog.show();
            return true;
        } else {
        }
        return super.onPreferenceTreeClick(preferenceScreen, preference);
    }

    @Override
    public void onClick(DialogInterface dialog, int button) {
        if (button == POSITIVE_BUTTON && !((NoProxyIpDialog) dialog).isEdit()) {
            if (!"".equals(((NoProxyIpDialog) dialog).getAddNoProxyIp())) {
////                if (!this.isCheck()) {
//                    Toast.makeText(this, R.string.format_check,
//                            Toast.LENGTH_SHORT).show();
//                } else {
                    NoProxyIp mNoProxyIp = new NoProxyIp(this,
                            ((NoProxyIpDialog) dialog).getAddNoProxyIp());
                    mIpList.addPreference(mNoProxyIp);
//                }
            }
        } else if (button == POSITIVE_BUTTON
                && ((NoProxyIpDialog) dialog).isEdit()) {
            if (!"".equals(((NoProxyIpDialog) dialog).getAddNoProxyIp())) {

//                if (!this.isCheck()) {
//                    Toast.makeText(this, R.string.format_check,
//                            Toast.LENGTH_SHORT).show();
//                } else {
                    mTempPreference.setIpString(((NoProxyIpDialog) dialog)
                            .getAddNoProxyIp());
                    mIpList.removePreference(mTempPreference);
                    mIpList.addPreference(mTempPreference);
//                }
            }
        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View view,
            ContextMenuInfo info) {
        Preference preference = (Preference) getListView().getItemAtPosition(
                ((AdapterContextMenuInfo) info).position);
        if (preference instanceof NoProxyIp) {
            mTempPreference = (NoProxyIp) preference;
            menu.add(Menu.NONE, EDIT, 0, R.string.no_proxy_edit);
            menu.add(Menu.NONE, DELETE, 0, R.string.no_proxy_delete);
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case EDIT: {
            NoProxyIpDialog mNoProxyIpDialog = new NoProxyIpDialog(this, this,
                    ((NoProxyIp) mTempPreference).getIpString());
            mNoProxyIpDialog.show();
            break;
        }
        case DELETE: {
            mIpList.removePreference(mTempPreference);
            break;
        }
        }
        return true;
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
////        if (!this.isCheck()) {
//            Toast.makeText(this, R.string.format_check, Toast.LENGTH_SHORT)
//                    .show();
//            return false;
//        } else {
            preference.setSummary((String) newValue);
            return true;
//        }
    }

    public boolean isCheck() {
        return false;
    }
}
